<!DOCTYPE html>
<html lang="zh-cn" dir="ltr">
    <head><script src="/livereload.js?mindelay=10&amp;v=2&amp;port=1313&amp;path=livereload" data-no-instant defer></script><meta charset='utf-8'>
<meta name='viewport' content='width=device-width, initial-scale=1'><meta name='description' content="见证菜狗的积累">
<title>C&#43;&#43;语法笔记</title>

<link rel='canonical' href='http://localhost:1313/p/c-/'>

<link rel="stylesheet" href="/scss/style.min.663803bebe609202d5b39d848f2d7c2dc8b598a2d879efa079fa88893d29c49c.css"><meta property='og:title' content="C++语法笔记">
<meta property='og:description' content="见证菜狗的积累">
<meta property='og:url' content='http://localhost:1313/p/c-/'>
<meta property='og:site_name' content='VenKing_Ryao的个人博客'>
<meta property='og:type' content='article'><meta property='article:section' content='Post' /><meta property='article:published_time' content='2024-11-04T00:00:00&#43;00:00'/><meta property='article:modified_time' content='2024-11-04T00:00:00&#43;00:00'/><meta property='og:image' content='http://localhost:1313/images/code1.jpg' />
<meta name="twitter:title" content="C++语法笔记">
<meta name="twitter:description" content="见证菜狗的积累"><meta name="twitter:card" content="summary_large_image">
    <meta name="twitter:image" content='http://localhost:1313/images/code1.jpg' />
    <link rel="shortcut icon" href="/favicon.ico" />

  


    </head>
    <body class="
    article-page
    ">
    <script>
        (function() {
            const colorSchemeKey = 'StackColorScheme';
            if(!localStorage.getItem(colorSchemeKey)){
                localStorage.setItem(colorSchemeKey, "auto");
            }
        })();
    </script><script>
    (function() {
        const colorSchemeKey = 'StackColorScheme';
        const colorSchemeItem = localStorage.getItem(colorSchemeKey);
        const supportDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches === true;

        if (colorSchemeItem == 'dark' || colorSchemeItem === 'auto' && supportDarkMode) {
            

            document.documentElement.dataset.scheme = 'dark';
        } else {
            document.documentElement.dataset.scheme = 'light';
        }
    })();
</script>
<div class="container main-container flex on-phone--column extended"><aside class="sidebar left-sidebar sticky ">
    <button class="hamburger hamburger--spin" type="button" id="toggle-menu" aria-label="切换菜单">
        <span class="hamburger-box">
            <span class="hamburger-inner"></span>
        </span>
    </button>

    <header>
        
            
            <figure class="site-avatar">
                <a href="/">
                
                    
                    
                    
                        
                        <img src="/img/avatar_hu12578812116692384380.png" width="300"
                            height="300" class="site-logo" loading="lazy" alt="Avatar">
                    
                
                </a>
                
                    <span class="emoji">🤔</span>
                
            </figure>
            
        
        
        <div class="site-meta">
            <h1 class="site-name"><a href="/">VenKing_Ryao的个人博客</a></h1>
            <h2 class="site-description">无限进步</h2>
        </div>
    </header><ol class="menu-social">
            
                <li>
                    <a 
                        href='https://github.com/RyaoVen'
                        target="_blank"
                        title="GitHub"
                        rel="me"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-github" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M9 19c-4.3 1.4 -4.3 -2.5 -6 -3m12 5v-3.5c0 -1 .1 -1.4 -.5 -2c2.8 -.3 5.5 -1.4 5.5 -6a4.6 4.6 0 0 0 -1.3 -3.2a4.2 4.2 0 0 0 -.1 -3.2s-1.1 -.3 -3.5 1.3a12.3 12.3 0 0 0 -6.2 0c-2.4 -1.6 -3.5 -1.3 -3.5 -1.3a4.2 4.2 0 0 0 -.1 3.2a4.6 4.6 0 0 0 -1.3 3.2c0 4.6 2.7 5.7 5.5 6c-.6 .6 -.6 1.2 -.5 2v3.5" />
</svg>



                        
                    </a>
                </li>
            
                <li>
                    <a 
                        href='https://twitter.com'
                        target="_blank"
                        title="Twitter"
                        rel="me"
                    >
                        
                        
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-brand-twitter" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M22 4.01c-1 .49 -1.98 .689 -3 .99c-1.121 -1.265 -2.783 -1.335 -4.38 -.737s-2.643 2.06 -2.62 3.737v1c-3.245 .083 -6.135 -1.395 -8 -4c0 0 -4.182 7.433 4 11c-1.872 1.247 -3.739 2.088 -6 2c3.308 1.803 6.913 2.423 10.034 1.517c3.58 -1.04 6.522 -3.723 7.651 -7.742a13.84 13.84 0 0 0 .497 -3.753c-.002 -.249 1.51 -2.772 1.818 -4.013z" />
</svg>



                        
                    </a>
                </li>
            
        </ol><ol class="menu" id="main-menu">
        
        
        
        <li >
            <a href='/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-home" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <polyline points="5 12 3 12 12 3 21 12 19 12" />
  <path d="M5 12v7a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-7" />
  <path d="M9 21v-6a2 2 0 0 1 2 -2h2a2 2 0 0 1 2 2v6" />
</svg>



                
                <span>主页</span>
            </a>
        </li>
        
        
        <li >
            <a href='/%E5%85%B3%E4%BA%8E/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="7" r="4" />
  <path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" />
</svg>



                
                <span>关于</span>
            </a>
        </li>
        
        
        <li >
            <a href='/archives/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <rect x="3" y="4" width="18" height="4" rx="2" />
  <path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" />
  <line x1="10" y1="12" x2="14" y2="12" />
</svg>



                
                <span>归档</span>
            </a>
        </li>
        
        
        <li >
            <a href='/search/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-search" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="10" cy="10" r="7" />
  <line x1="21" y1="21" x2="15" y2="15" />
</svg>



                
                <span>搜索</span>
            </a>
        </li>
        
        
        <li >
            <a href='/%E7%9B%B8%E5%85%B3%E8%B6%85%E9%93%BE%E6%8E%A5/' >
                
                
                
                    <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-link" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" />
  <path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" />
</svg>



                
                <span>相关超链接</span>
            </a>
        </li>
        
        <li class="menu-bottom-section">
            <ol class="menu">
                    
                        <li id="i18n-switch">  
                            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-language" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z" fill="none"/>
  <path d="M4 5h7" />
  <path d="M9 3v2c0 4.418 -2.239 8 -5 8" />
  <path d="M5 9c-.003 2.144 2.952 3.908 6.7 4" />
  <path d="M12 20l4 -9l4 9" />
  <path d="M19.1 18h-6.2" />
</svg>



                            <select name="language" title="language" onchange="window.location.href = this.selectedOptions[0].value">
                                
                                    <option value="http://localhost:1313/en/" >English</option>
                                
                                    <option value="http://localhost:1313/" selected>简体中文</option>
                                
                            </select>
                        </li>
                    
                

                
                    <li id="dark-mode-toggle">
                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-left" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="8" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-toggle-right" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="16" cy="12" r="2" />
  <rect x="2" y="6" width="20" height="12" rx="6" />
</svg>



                        <span>暗色模式</span>
                    </li>
                
            </ol>
        </li>
    </ol>
</aside>

    <aside class="sidebar right-sidebar sticky">
        
            
                
    <section class="widget archives">
        <div class="widget-icon">
            <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-hash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <line x1="5" y1="9" x2="19" y2="9" />
  <line x1="5" y1="15" x2="19" y2="15" />
  <line x1="11" y1="4" x2="7" y2="20" />
  <line x1="17" y1="4" x2="13" y2="20" />
</svg>



        </div>
        <h2 class="widget-title section-title">目录</h2>
        
        <div class="widget--toc">
            <nav id="TableOfContents">
  <ol>
    <li><a href="#文件的读写">文件的读写</a>
      <ol>
        <li><a href="#c用二进制写入读取文件与普通方法写入读取文件的区别">c++用二进制写入读取文件与普通方法写入读取文件的区别</a>
          <ol>
            <li><a href="#font-stylebackground-colorrgba255-255-255-0性能和效率font"><font style="background-color:rgba(255, 255, 255, 0);">性能和效率</font></a></li>
            <li><a href="#font-stylebackground-colorrgba255-255-255-0使用场景font"><font style="background-color:rgba(255, 255, 255, 0);">使用场景</font></a></li>
          </ol>
        </li>
        <li><a href="#读取文件的四种基本方法">读取文件的四种基本方法</a>
          <ol>
            <li><a href="#处理大量数据时的读取方法效率比较">处理大量数据时的读取方法效率比较</a></li>
            <li><a href="#结论">结论</a></li>
          </ol>
        </li>
        <li><a href="#cmd控制台读取文件乱码解决方案">cmd控制台读取文件乱码解决方案</a></li>
        <li><a href="#c文件读写操作基础语法">c++文件读写操作基础语法</a></li>
      </ol>
    </li>
    <li><a href="#难点语法">难点语法</a>
      <ol>
        <li><a href="#构造函数基本原则">构造函数基本原则</a></li>
        <li><a href="#指针基本语法">指针基本语法</a></li>
        <li><a href="#动态数组自动扩容">动态数组自动扩容</a></li>
      </ol>
    </li>
    <li><a href="#面向对象">面向对象</a>
      <ol>
        <li><a href="#类继承示例与说明">类继承示例与说明</a>
          <ol>
            <li><a href="#类定义与继承">类定义与继承</a></li>
            <li><a href="#成员继承与访问权限">成员继承与访问权限</a></li>
            <li><a href="#构造函数与析构函数的调用顺序">构造函数与析构函数的调用顺序</a></li>
            <li><a href="#成员隐藏与访问">成员隐藏与访问</a></li>
            <li><a href="#多重继承与菱形继承">多重继承与菱形继承</a></li>
            <li><a href="#虚析构函数">虚析构函数</a></li>
          </ol>
        </li>
        <li><a href="#多态性与虚函数">多态性与虚函数</a>
          <ol>
            <li><a href="#虚函数的定义和工作原理">虚函数的定义和工作原理</a></li>
            <li><a href="#示例代码-1">示例代码</a></li>
          </ol>
        </li>
        <li><a href="#判断函数运算符重载与继承基础语法示例">判断、函数运算符重载与继承基础语法示例</a>
          <ol>
            <li><a href="#代码">代码</a></li>
          </ol>
        </li>
        <li><a href="#运算符重载">运算符重载</a>
          <ol>
            <li><a href="#解释">解释</a></li>
            <li><a href="#注意事项">注意事项</a></li>
            <li><a href="#总结">总结</a></li>
          </ol>
        </li>
        <li><a href="#全局函数友元">全局函数友元</a></li>
      </ol>
    </li>
    <li><a href="#stl容器简略写点">STL容器（简略写点）</a>
      <ol>
        <li><a href="#基本语法">基本语法</a></li>
        <li><a href="#stl容器的一些示例">STL容器的一些示例</a>
          <ol>
            <li><a href="#大数阶乘容器">大数阶乘（容器）</a></li>
            <li><a href="#基于vector容器的素数查找">基于vector容器的素数查找</a></li>
          </ol>
        </li>
      </ol>
    </li>
  </ol>
</nav>
        </div>
    </section>

            
        
    </aside>


            <main class="main full-width">
    <article class="has-image main-article">
    <header class="article-header">
        <div class="article-image">
            <a href="/p/c-/">
                
                    <img src="/images/code1.jpg" loading="lazy" alt="Featured image of post C&#43;&#43;语法笔记" />
                
            </a>
        </div>
    

    <div class="article-details">
    
    <header class="article-category">
        
            <a href="/categories/%E7%AC%94%E8%AE%B0/" >
                笔记
            </a>
        
            <a href="/categories/c&#43;&#43;/" >
                C&#43;&#43;
            </a>
        
            <a href="/categories/ryao_venking/" >
                Ryao_VenKing
            </a>
        
    </header>
    

    <div class="article-title-wrapper">
        <h2 class="article-title">
            <a href="/p/c-/">C&#43;&#43;语法笔记</a>
        </h2>
    
        
        <h3 class="article-subtitle">
            见证菜狗的积累
        </h3>
        
    </div>

    
    
    
    
    <footer class="article-time">
        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-calendar-time" width="56" height="56" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <path d="M11.795 21h-6.795a2 2 0 0 1 -2 -2v-12a2 2 0 0 1 2 -2h12a2 2 0 0 1 2 2v4" />
  <circle cx="18" cy="18" r="4" />
  <path d="M15 3v4" />
  <path d="M7 3v4" />
  <path d="M3 11h16" />
  <path d="M18 16.496v1.504l1 1" />
</svg>
                <time class="article-time--published">Nov 04, 2024</time>
            </div>
        

        
            <div>
                <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-clock" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <polyline points="12 7 12 12 15 15" />
</svg>



                <time class="article-time--reading">
                    阅读时长: 11 分钟
                </time>
            </div>
        
    </footer>
    

    
</div>

</header>

    <section class="article-content">
    
    
    <p><strong>前言：</strong></p>
<p>虽然许多人都说c++是最难学的语言，笔者也的确承认这一点，但是笔者认为，c++反而是最能锻炼一个人程序编写能力的语言。c++将内存之类的一般由编译器控制的结构开放给程序员，使其拥有对程序更为慎密操作能力。虽然对大多数初学者来说，其极易出错的各类复杂语法简直就是噩梦，但是当养成了精细控制内存的好习惯后，再去使用其他语言时，则会有一种别样的体验。<del>为什么我早不学这玩意，硬是要去啃c++这反人类的垃圾东西（红温症状）</del>，拥有一种别样的舒适感。</p>
<p>笔记中仅仅记录了些许难点，深点与在经常使用c++进行面向过程编程时容易遗忘的点。（拿c++面向对象写大东西时候用的）</p>
<p>如果要系统性的学习c++，笔者建议去b站看些黑马程序员的课，笔记作为补充或强调的一部分。</p>
<h2 id="文件的读写">文件的读写
</h2><h3 id="c用二进制写入读取文件与普通方法写入读取文件的区别">c++用二进制写入读取文件与普通方法写入读取文件的区别
</h3><h4 id="font-stylebackground-colorrgba255-255-255-0性能和效率font"><font style="background-color:rgba(255, 255, 255, 0);">性能和效率</font>
</h4><ul>
<li><strong><font style="background-color:rgba(255, 255, 255, 0);">性能</font></strong><font style="background-color:rgba(255, 255, 255, 0);">：二进制模式通常比文本模式更快，因为它避免了字符编码和解码的开销，以及格式化和转换的开销。</font></li>
<li><strong><font style="background-color:rgba(255, 255, 255, 0);">效率</font></strong><font style="background-color:rgba(255, 255, 255, 0);">：二进制模式可以更有效地存储数据，因为它不需要额外的空间来存储字符编码，尤其是在处理大型数据集时。</font></li>
</ul>
<h4 id="font-stylebackground-colorrgba255-255-255-0使用场景font"><font style="background-color:rgba(255, 255, 255, 0);">使用场景</font>
</h4><ul>
<li><strong><font style="background-color:rgba(255, 255, 255, 0);">文本模式</font></strong><font style="background-color:rgba(255, 255, 255, 0);">：当你需要处理的是文本数据，并且需要跨平台兼容（比如处理换行符）时，使用文本模式是合适的。</font></li>
<li><strong><font style="background-color:rgba(255, 255, 255, 0);">二进制模式</font></strong><font style="background-color:rgba(255, 255, 255, 0);">：当你需要处理非文本数据，或者需要确保数据的精确二进制表示时（比如图像、音频、视频文件或者自定义的数据结构），使用二进制模式是更合适的。</font></li>
</ul>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span><span class="lnt">44
</span><span class="lnt">45
</span><span class="lnt">46
</span><span class="lnt">47
</span><span class="lnt">48
</span><span class="lnt">49
</span><span class="lnt">50
</span><span class="lnt">51
</span><span class="lnt">52
</span><span class="lnt">53
</span><span class="lnt">54
</span><span class="lnt">55
</span><span class="lnt">56
</span><span class="lnt">57
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="c1">// 定义一个简单的结构体
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="k">struct</span> <span class="nc">Person</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">std</span><span class="o">::</span><span class="n">string</span> <span class="n">name</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">age</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">writeBinaryFile</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">filename</span><span class="p">,</span> <span class="k">const</span> <span class="n">Person</span><span class="o">&amp;</span> <span class="n">person</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">ofs</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="p">);</span> <span class="c1">// 以二进制模式打开文件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ofs</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;无法打开文件进行写入&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// 写入数据
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">ofs</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">person</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">c_str</span><span class="p">(),</span> <span class="n">person</span><span class="p">.</span><span class="n">name</span><span class="p">.</span><span class="n">size</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span> <span class="c1">// 写入姓名和终止符
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">ofs</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="k">reinterpret_cast</span><span class="o">&lt;</span><span class="k">const</span> <span class="kt">char</span><span class="o">*&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="n">person</span><span class="p">.</span><span class="n">age</span><span class="p">),</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">person</span><span class="p">.</span><span class="n">age</span><span class="p">));</span> <span class="c1">// 写入年龄
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ofs</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;写入文件时发生错误&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">readBinaryFile</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">filename</span><span class="p">,</span> <span class="n">Person</span><span class="o">&amp;</span> <span class="n">person</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">std</span><span class="o">::</span><span class="n">ifstream</span> <span class="n">ifs</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">binary</span><span class="p">);</span> <span class="c1">// 以二进制模式打开文件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ifs</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;无法打开文件进行读取&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// 读取数据
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="kt">char</span> <span class="n">nameBuffer</span><span class="p">[</span><span class="mi">100</span><span class="p">];</span> <span class="c1">// 假设姓名不超过99个字符
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">ifs</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="n">nameBuffer</span><span class="p">,</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">nameBuffer</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">nameBuffer</span><span class="p">[</span><span class="n">ifs</span><span class="p">.</span><span class="n">gcount</span><span class="p">()]</span> <span class="o">=</span> <span class="sc">&#39;\0&#39;</span><span class="p">;</span> <span class="c1">// 确保字符串以null终止
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">person</span><span class="p">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">nameBuffer</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">ifs</span><span class="p">.</span><span class="n">read</span><span class="p">(</span><span class="k">reinterpret_cast</span><span class="o">&lt;</span><span class="kt">char</span><span class="o">*&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="n">person</span><span class="p">.</span><span class="n">age</span><span class="p">),</span> <span class="k">sizeof</span><span class="p">(</span><span class="n">person</span><span class="p">.</span><span class="n">age</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">ifs</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;读取文件时发生错误&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="n">ifs</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">Person</span> <span class="n">personToWrite</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#34;John Doe&#34;</span><span class="p">,</span> <span class="mi">30</span><span class="p">};</span>
</span></span><span class="line"><span class="cl">    <span class="n">Person</span> <span class="n">personToRead</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">writeBinaryFile</span><span class="p">(</span><span class="s">&#34;person.dat&#34;</span><span class="p">,</span> <span class="n">personToWrite</span><span class="p">);</span> <span class="c1">// 写入文件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl">    <span class="n">readBinaryFile</span><span class="p">(</span><span class="s">&#34;person.dat&#34;</span><span class="p">,</span> <span class="n">personToRead</span><span class="p">);</span> <span class="c1">// 读取文件
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl">    <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;姓名: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">personToRead</span><span class="p">.</span><span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;, 年龄: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">personToRead</span><span class="p">.</span><span class="n">age</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="读取文件的四种基本方法">读取文件的四种基本方法
</h3><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="kt">char</span> <span class="n">arr</span><span class="p">[</span><span class="mi">1000</span><span class="p">]</span><span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="n">ifs</span><span class="o">&gt;&gt;</span><span class="n">arr</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">arr</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="kt">char</span> <span class="n">arr</span><span class="p">[</span><span class="mi">1000</span><span class="p">]</span><span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="n">ifs</span><span class="p">.</span><span class="n">getline</span><span class="p">(</span><span class="n">arr</span><span class="p">,</span><span class="k">sizeof</span><span class="p">(</span><span class="n">arr</span><span class="p">)))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">arr</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span><span class="c1">//ifstream的ifs.getline需输入变量名与容量
</span></span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;string&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="n">string</span> <span class="n">line</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">(</span><span class="n">getline</span><span class="p">(</span><span class="n">ifs</span><span class="p">,</span><span class="n">line</span><span class="p">))</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">line</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span><span class="c1">//使用iostream自带的getline需输入数据流类型与变量名
</span></span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="kt">char</span> <span class="n">c</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">while</span><span class="p">((</span><span class="n">c</span> <span class="o">=</span> <span class="n">ifs</span><span class="p">.</span><span class="n">get</span><span class="p">())</span> <span class="o">!=</span> <span class="n">EOF</span><span class="p">)</span> <span class="p">{</span><span class="c1">//EOF是文件尾的意思（end of file）
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">c</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span><span class="c1">//ifs.get每次读取一个字符
</span></span></span></code></pre></td></tr></table>
</div>
</div><h4 id="处理大量数据时的读取方法效率比较">处理大量数据时的读取方法效率比较
</h4><p>处理大量数据时，选择最有效的读取方法取决于数据的特性和程序的需求。以下是对四种读取方法的效率比较：</p>
<ol>
<li>
<p><strong>使用 &raquo; 操作符</strong>：</p>
<ul>
<li>效率：较低</li>
<li>原因：<code>&gt;&gt;</code> 操作符会跳过空白字符（如空格、制表符和换行符），只读取直到下一个空白字符之前的字符串。如果数据中包含大量的空白字符，这种方法会导致频繁的读取操作，效率较低。</li>
</ul>
</li>
<li>
<p><strong>使用 <code>ifs.getline(arr, sizeof(arr))</code></strong>：</p>
<ul>
<li>效率：中等</li>
<li>原因：<code>getline</code> 函数读取整行数据，直到遇到换行符或达到缓冲区大小。这种方法适用于读取固定长度的行，但如果行长度不一，可能会导致不必要的内存拷贝和缓冲区溢出。</li>
</ul>
</li>
<li>
<p><strong>使用 <code>string</code> 和 <code>getline(ifs, line)</code></strong>：</p>
<ul>
<li>效率：较高</li>
<li>原因：这种方法使用 <code>string</code> 类型的变量来存储每行数据，<code>getline</code> 函数会自动管理内存，只分配所需的空间。这种方法对于处理长度不一的行数据非常有效，因为它可以动态调整内存分配。</li>
</ul>
</li>
<li>
<p><strong>使用 <code>ifs.get(c)</code></strong>：</p>
<ul>
<li>效率：最低</li>
<li>原因：<code>get</code> 函数每次只读取一个字符，这对于处理大量数据来说是非常低效的，因为它涉及到大量的函数调用和可能的内存访问。</li>
</ul>
</li>
</ol>
<h4 id="结论">结论
</h4><ul>
<li>如果输入流中的数据量非常大，且每行的长度大致相同，使用 <code>ifs.getline(arr, sizeof(arr))</code> 方法可能更有效，因为它可以一次性读取整行数据，减少了函数调用的次数。</li>
<li>如果输入流中的数据量非常大，且每行的长度不一，使用 <code>string</code> 和 <code>getline(ifs, line)</code> 方法可能更有效，因为 <code>string</code> 类型可以动态管理内存，适应不同长度的行数据。</li>
</ul>
<p>在处理大量数据时，还应该考虑其他因素，如内存使用、CPU缓存效率和I/O操作的开销。通常，减少I/O操作的次数和优化内存使用可以提高程序的整体效率。</p>
<h3 id="cmd控制台读取文件乱码解决方案">cmd控制台读取文件乱码解决方案
</h3><p>将当前文本文件另存为在当前目录下，将之前那个覆盖即可。</p>
<p><img src="/images/123.png"
	
	
	
	loading="lazy"
	
		alt="tp"
	
	
>
<img src="/images/456.png"
	
	
	
	loading="lazy"
	
		alt="tp"
	
	
></p>
<h3 id="c文件读写操作基础语法">c++文件读写操作基础语法
</h3><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofstream</span> <span class="n">ofs</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="p">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#34;test.txt&#34;</span><span class="p">,</span><span class="n">ios</span><span class="o">::</span><span class="n">out</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="o">&lt;&lt;</span><span class="s">&#34;张三&#34;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="o">&lt;&lt;</span><span class="s">&#34;李四&#34;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="o">&lt;&lt;</span><span class="s">&#34;王五&#34;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">ofs</span><span class="p">.</span><span class="n">close</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">test01</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>1.若需对文件进行读写操作，需先包含库<fstream>(文件操作流库)，基础语法如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl">   <span class="cp">#include</span> <span class="cpf">&lt;fstream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">del_file</span><span class="p">(</span><span class="k">const</span> <span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="o">&amp;</span> <span class="n">filename</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">std</span><span class="o">::</span><span class="n">ofstream</span> <span class="n">file</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">out</span> <span class="o">|</span> <span class="n">std</span><span class="o">::</span><span class="n">ios</span><span class="o">::</span><span class="n">trunc</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="n">file</span><span class="p">.</span><span class="n">is_open</span><span class="p">())</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">file</span><span class="p">.</span><span class="n">close</span><span class="p">();</span> <span class="c1">// 打开文件并立即关闭，这将清空文件内容
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">std</span><span class="o">::</span><span class="n">cerr</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;Unable to open file &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">filename</span> <span class="o">&lt;&lt;</span> <span class="n">std</span><span class="o">::</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h2 id="难点语法">难点语法
</h2><h3 id="构造函数基本原则">构造函数基本原则
</h3><ol>
<li>与类名相同，没有返回类型（包括void）。</li>
<li>可以有参数，用于给对象的数据成员赋初值。</li>
<li>可以重载，可以有多个构造函数，根据参数的不同进行区分。</li>
<li>默认构造函数是没有参数的构造函数，如果没有定义任何构造函数，C++会自动提供一个默认构造函数，默认构造函数不做任何操作。</li>
<li>构造函数可以被显式地调用，也可以被隐式地调用。</li>
</ol>
<p>调用原则：</p>
<p>对象创建时自动调用：</p>
<p>构造函数在创建对象时自动被调用，无需手动调用。</p>
<p>构造函数重载匹配：</p>
<p>如果类定义了多个构造函数，编译器会根据传递的参数来匹配合适的构造函数进行调用。</p>
<p>默认构造函数的调用：</p>
<p>如果没有显式定义构造函数，编译器会提供一个默认构造函数，用于创建对象并将成员变量初始化为默认值。</p>
<p>显式调用构造函数：</p>
<p>可以显式地调用构造函数来创建对象，即使用类名后跟括号和参数列表的形式进行调用。</p>
<p>初始化列表的使用：</p>
<p>构造函数可以使用初始化列表来初始化成员变量，这样可以更高效地初始化对象的数据成员。初始化列表位于构造函数的函数体之前。</p>
<p>基类构造函数的调用：</p>
<p>派生类的构造函数会自动调用基类的构造函数来初始化继承的成员。可以使用初始化列表来指定基类构造函数的参数。</p>
<p>析构函数的调用：</p>
<p>析构函数在对象被销毁时自动调用，用于执行清理工作，例如释放动态分配的内存或关闭打开的文件。</p>
<h3 id="指针基本语法">指针基本语法
</h3><p><strong><font style="background-color:rgb(255, 250, 165);">int a = 0</font></strong></p>
<p><strong><font style="background-color:rgb(255, 250, 165);">//声明指针</font></strong></p>
<p><strong><font style="background-color:rgb(255, 250, 165);">int * p;</font></strong></p>
<p><strong><font style="background-color:rgb(255, 250, 165);">//记录地址</font></strong></p>
<p><strong><font style="background-color:rgb(255, 250, 165);">p = &amp;a</font></strong></p>
<p><strong><font style="background-color:rgb(255, 250, 165);">//使用指针</font></strong></p>
<p>*<em><font style="background-color:rgb(255, 250, 165);"><em>p = 10000</font></em></em></p>
<p>*<em><font style="background-color:rgb(255, 250, 165);">cout&laquo;<em>p&laquo;;</font></em></em></p>
<p><strong><font style="background-color:rgb(255, 250, 165);"><br>
</font></strong></p>
<p><font style="color:rgb(50, 135, 18);">int *p = nullptr(空指针，用于给指针清空)</font></p>
<p><font style="color:rgb(50, 135, 18);">野指针指指向地址非法的指针</font></p>
<p><font style="color:rgb(50, 135, 18);">const 在 int * 前时构成常量指针，</font><font style="color:rgb(6, 6, 7);">常量指针是指指针指向的值不能改变，但可以改变指针的指向。</font></p>
<p><font style="color:rgb(6, 6, 7);">const在 int * 后时构成指针常量， 指针常量指向的量不能改变，但量的值能够改变</font></p>
<p><font style="color:rgb(6, 6, 7);">const在int * 前后都有时构成常量指针常量，他指向的值与值本身都不能改变</font></p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt; 
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span> 
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// 定义一个名为myclass的类
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">age</span><span class="p">;</span> <span class="c1">// 类的成员变量，用于存储年龄
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 类的构造函数，用于初始化对象的age成员变量
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span><span class="p">(</span><span class="kt">int</span> <span class="n">age</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">this</span><span class="o">-&gt;</span><span class="n">age</span> <span class="o">=</span> <span class="n">age</span><span class="p">;</span> <span class="c1">// 将传入的age值赋给对象的age成员变量
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="c1">// 类的成员函数，用于将传入对象的age值加到当前对象的age上，并返回当前对象的引用
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span><span class="o">&amp;</span> <span class="n">numadd</span><span class="p">(</span><span class="n">myclass</span> <span class="o">&amp;</span><span class="n">p</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">this</span><span class="o">-&gt;</span><span class="n">age</span> <span class="o">+=</span> <span class="n">p</span><span class="p">.</span><span class="n">age</span><span class="p">;</span> <span class="c1">// 将传入对象的age值加到当前对象的age上
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="k">return</span> <span class="o">*</span><span class="k">this</span><span class="p">;</span> <span class="c1">// 返回当前对象的引用，这样可以进行链式调用
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 创建一个myclass类型的对象p1，并初始化其age为10
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span> <span class="n">p1</span><span class="p">(</span><span class="mi">10</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 使用p1对象创建另一个myclass类型的对象p2
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span> <span class="n">p2</span><span class="p">(</span><span class="n">p1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 使用链式编程思想，将p1对象的age值连续三次加到p2对象的age上
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">p2</span><span class="p">.</span><span class="n">numadd</span><span class="p">(</span><span class="n">p1</span><span class="p">).</span><span class="n">numadd</span><span class="p">(</span><span class="n">p1</span><span class="p">).</span><span class="n">numadd</span><span class="p">(</span><span class="n">p1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 输出p2对象的age值
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">p2</span><span class="p">.</span><span class="n">age</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>*this 指代当前对象的引用</p>
<p>this-&gt;指针指代当前类中的成员变量</p>
<p>空指针也可以访问成员函数</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="n">age</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="kt">void</span> <span class="nf">print</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">		<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;this is null.&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">	<span class="n">myclass</span><span class="o">*</span> <span class="n">null</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="n">null</span><span class="o">-&gt;</span><span class="n">print</span><span class="p">();</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>但当成员函数中存在成员变量的调用时，空指针指向的成员函数会报错.</p>
<blockquote>
<p><font style="background-color:#FFFFFF;">析构函数的特例</font></p>
<p><font style="background-color:#FFFFFF;">当一个对象被销毁时，如果它的析构函数被调用，并且这个对象是通过空指针访问的，C++运行时会允许这次调用。这是因为析构函数的调用是为了释放资源，即使对象已经被销毁，析构函数也需要能够执行。这个特例只在析构函数中有效，并且仅限于对象的销毁过程。</font></p>
</blockquote>
<h3 id="动态数组自动扩容">动态数组自动扩容
</h3><p>int* arr = new int[初始大小]</p>
<p>if(数组存满){</p>
<p>int arrNew = new int[原容量 * 2]</p>
<p>for(int a = 0;a&lt;原容量;a++)</p>
<p>{</p>
<p>arrNew[a] = arr[a];</p>
<p>}                        <font style="color:rgb(150, 238, 125);">//用于转移数组内存储数据  </font></p>
<p>delete[] arr;      <font style="color:rgb(150, 238, 125);">//用于释放原有数组内存</font></p>
<p>arr = arrNew;   <font style="color:rgb(150, 238, 125);">//用于重新指向arr指针位置</font></p>
<h2 id="面向对象">面向对象
</h2><h3 id="类继承示例与说明">类继承示例与说明
</h3><h4 id="类定义与继承">类定义与继承
</h4><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">DadClass</span> <span class="p">{};</span>
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">SonClass</span> <span class="o">:</span> <span class="k">public</span> <span class="n">DadClass</span> <span class="p">{};</span>
</span></span></code></pre></td></tr></table>
</div>
</div><ul>
<li><code>SonClass</code> 通过 <code>public</code> 继承自 <code>DadClass</code>，这意味着 <code>SonClass</code> 将继承 <code>DadClass</code> 的所有 <code>public</code> 和 <code>protected</code> 成员。</li>
</ul>
<h4 id="成员继承与访问权限">成员继承与访问权限
</h4><ol>
<li><strong>Public 继承</strong>：将父类的 <code>public</code> 和 <code>protected</code> 成员继承为子类的 <code>public</code> 和 <code>protected</code> 成员（<code>private</code> 成员不可访问）。</li>
<li><strong>Protected 继承</strong>：将父类的 <code>public</code> 和 <code>protected</code> 成员继承为子类的 <code>protected</code> 成员。</li>
<li><strong>Private 继承</strong>：将父类的 <code>public</code> 和 <code>protected</code> 成员继承为子类的 <code>private</code> 成员。</li>
</ol>
<h4 id="构造函数与析构函数的调用顺序">构造函数与析构函数的调用顺序
</h4><ul>
<li>当创建子类对象时，首先调用父类的构造函数，然后调用子类的构造函数。</li>
<li>当销毁子类对象时，首先调用子类的析构函数，然后调用父类的析构函数。</li>
</ul>
<h4 id="成员隐藏与访问">成员隐藏与访问
</h4><p>当子类与父类有同名成员时，子类成员会隐藏父类成员。要访问被隐藏的父类成员，需要使用作用域解析运算符（<code>::</code>）。</p>
<h5 id="示例代码">示例代码
</h5><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;string&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">FatherClass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">string</span> <span class="n">name</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">FatherClass</span><span class="p">()</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="s">&#34;Here is FatherClass&#34;</span><span class="p">)</span> <span class="p">{}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">SonClass</span> <span class="o">:</span> <span class="k">public</span> <span class="n">FatherClass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">string</span> <span class="n">name</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">SonClass</span><span class="p">()</span> <span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="s">&#34;Here is SonClass&#34;</span><span class="p">)</span> <span class="p">{}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">FatherClass</span> <span class="n">f1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">SonClass</span> <span class="n">s1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;f1.name = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">f1</span><span class="p">.</span><span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;s1.name = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s1</span><span class="p">.</span><span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;s1&#39;s FatherClass::name = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">s1</span><span class="p">.</span><span class="n">FatherClass</span><span class="o">::</span><span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">test01</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// system(&#34;pause&#34;); // 通常在非Windows系统上不需要此行
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="多重继承与菱形继承">多重继承与菱形继承
</h4><p>C++ 支持多重继承，但如果多个父类有同名成员，访问时需要指定作用域。菱形继承可能导致二义性，为避免这种情况，可以使用虚继承。</p>
<h5 id="菱形继承示例">菱形继承示例
</h5><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">AnotherClass</span> <span class="p">{};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">SonClass2</span> <span class="o">:</span> <span class="k">virtual</span> <span class="k">public</span> <span class="n">FatherClass</span><span class="p">,</span> <span class="k">public</span> <span class="n">AnotherClass</span> <span class="p">{};</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>在菱形继承中，<code>SonClass2</code> 通过虚继承从 <code>FatherClass</code> 继承，以避免由于 <code>FatherClass</code> 被多次继承而导致的成员变量重复定义问题。</p>
<h4 id="虚析构函数">虚析构函数
</h4><p>当子类在堆上分配内存时，如果父类的析构函数不是虚函数，那么删除子类对象时不会调用子类的析构函数，从而导致内存泄漏。为避免这种情况，应将父类的析构函数声明为虚函数。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">Base</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="k">virtual</span> <span class="o">~</span><span class="n">Base</span><span class="p">()</span> <span class="p">{}</span> <span class="c1">// 虚析构函数
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>如果希望父类成为抽象类，可以将析构函数声明为纯虚函数，但需要在类外提供实现。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span><span class="lnt">6
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">AbstractBase</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="k">virtual</span> <span class="o">~</span><span class="n">AbstractBase</span><span class="p">()</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="c1">// 纯虚析构函数声明
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="n">AbstractBase</span><span class="o">::~</span><span class="n">AbstractBase</span><span class="p">()</span> <span class="p">{}</span> <span class="c1">// 纯虚析构函数实现
</span></span></span></code></pre></td></tr></table>
</div>
</div><h3 id="多态性与虚函数">多态性与虚函数
</h3><h4 id="虚函数的定义和工作原理">虚函数的定义和工作原理
</h4><p>在父类中的引用函数想要在子类中顺利调用，可以将函数定义为虚函数。</p>
<h5 id="虚函数表vtable">虚函数表（VTable）
</h5><ul>
<li><strong>每个包含虚函数的类都有一个虚函数表（VTable）</strong>，这是一个函数指针数组。</li>
<li><strong>这个表在类的构造函数中被初始化</strong>，包含了指向该类中所有虚函数的指针。</li>
</ul>
<h5 id="虚函数指针vptr">虚函数指针（VPtr）
</h5><ul>
<li><strong>每个包含虚函数的对象都有一个虚函数表指针（VPtr）</strong>，它指向该对象所属类的虚函数表。</li>
<li><strong>当创建一个对象时，构造函数会设置这个指针</strong>，使其指向正确的虚函数表。</li>
</ul>
<h5 id="函数调用">函数调用
</h5><ul>
<li><strong>当通过对象调用一个虚函数时</strong>，编译器不会直接调用该函数，而是通过虚函数表指针查找对应的函数指针，然后调用该指针指向的函数。</li>
<li><strong>这意味着</strong>，即使通过基类的指针或引用调用虚函数，只要对象的实际类型是派生类，就会调用派生类中重写的函数。</li>
</ul>
<h4 id="示例代码-1">示例代码
</h4><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp">#include&lt;string&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">father</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">father</span><span class="p">()</span><span class="o">:</span> <span class="n">name</span><span class="p">(</span><span class="s">&#34;Dad&#34;</span><span class="p">)</span> <span class="p">{}</span>
</span></span><span class="line"><span class="cl">    <span class="n">string</span> <span class="n">name</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">virtual</span> <span class="kt">void</span> <span class="nf">printfatherclass</span><span class="p">(</span><span class="n">father</span><span class="o">&amp;</span> <span class="n">who</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is class&#39;s name: &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">who</span><span class="p">.</span><span class="n">name</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">son</span><span class="o">:</span> <span class="k">public</span> <span class="n">father</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">    <span class="n">son</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">name</span> <span class="o">=</span> <span class="s">&#34;Son&#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">father</span> <span class="n">a</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">a</span><span class="p">.</span><span class="n">printfatherclass</span><span class="p">(</span><span class="n">a</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="n">son</span> <span class="n">b</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">b</span><span class="p">.</span><span class="n">printfatherclass</span><span class="p">(</span><span class="n">b</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">test01</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="n">system</span><span class="p">(</span><span class="s">&#34;pause&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="判断函数运算符重载与继承基础语法示例">判断、函数运算符重载与继承基础语法示例
</h3><p>以下是一个C++程序，展示了如何进行判断、函数运算符重载以及继承的基础语法。</p>
<h4 id="代码">代码
</h4><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span><span class="lnt">106
</span><span class="lnt">107
</span><span class="lnt">108
</span><span class="lnt">109
</span><span class="lnt">110
</span><span class="lnt">111
</span><span class="lnt">112
</span><span class="lnt">113
</span><span class="lnt">114
</span><span class="lnt">115
</span><span class="lnt">116
</span><span class="lnt">117
</span><span class="lnt">118
</span><span class="lnt">119
</span><span class="lnt">120
</span><span class="lnt">121
</span><span class="lnt">122
</span><span class="lnt">123
</span><span class="lnt">124
</span><span class="lnt">125
</span><span class="lnt">126
</span><span class="lnt">127
</span><span class="lnt">128
</span><span class="lnt">129
</span><span class="lnt">130
</span><span class="lnt">131
</span><span class="lnt">132
</span><span class="lnt">133
</span><span class="lnt">134
</span><span class="lnt">135
</span><span class="lnt">136
</span><span class="lnt">137
</span><span class="lnt">138
</span><span class="lnt">139
</span><span class="lnt">140
</span><span class="lnt">141
</span><span class="lnt">142
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">  
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">bool</span> <span class="k">operator</span><span class="o">==</span><span class="p">(</span><span class="n">myclass</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">m_h</span> <span class="o">==</span> <span class="n">p</span><span class="p">.</span><span class="n">m_h</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_w</span> <span class="o">==</span> <span class="n">p</span><span class="p">.</span><span class="n">m_w</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="kt">bool</span> <span class="k">operator</span><span class="o">!=</span><span class="p">(</span><span class="k">const</span> <span class="n">myclass</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">m_h</span> <span class="o">==</span> <span class="n">p</span><span class="p">.</span><span class="n">m_h</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_w</span> <span class="o">==</span> <span class="n">p</span><span class="p">.</span><span class="n">m_w</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="kt">bool</span> <span class="k">operator</span><span class="o">&gt;</span><span class="p">(</span><span class="k">const</span> <span class="n">myclass</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="k">this</span><span class="o">-&gt;</span><span class="n">m_h</span> <span class="o">&gt;</span> <span class="n">p</span><span class="p">.</span><span class="n">m_h</span> <span class="o">&amp;&amp;</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_w</span> <span class="o">&gt;</span> <span class="n">p</span><span class="p">.</span><span class="n">m_w</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">true</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">            <span class="k">return</span> <span class="nb">false</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="p">}</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span><span class="p">(</span><span class="kt">int</span> <span class="n">num1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">num2</span><span class="p">)</span> <span class="o">:</span> <span class="n">m_h</span><span class="p">(</span><span class="mi">176</span><span class="p">),</span> <span class="n">m_w</span><span class="p">(</span><span class="mi">75</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">m_h</span> <span class="o">=</span> <span class="n">num1</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="n">m_w</span> <span class="o">=</span> <span class="n">num2</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span><span class="p">()</span> <span class="o">:</span> <span class="n">m_h</span><span class="p">(</span><span class="mi">176</span><span class="p">),</span> <span class="n">m_w</span><span class="p">(</span><span class="mi">75</span><span class="p">)</span> <span class="p">{}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl"><span class="k">private</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">m_h</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">m_w</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">AddNum</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="k">operator</span><span class="p">()(</span><span class="kt">int</span> <span class="n">num1</span><span class="p">,</span> <span class="kt">int</span> <span class="n">num2</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">num1</span> <span class="o">+</span> <span class="n">num2</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span> <span class="n">p1</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span> <span class="n">p2</span><span class="p">(</span><span class="mi">160</span><span class="p">,</span> <span class="mi">45</span><span class="p">);</span>  
</span></span><span class="line"><span class="cl">    <span class="k">if</span><span class="p">(</span><span class="n">p1</span> <span class="o">==</span> <span class="n">p2</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;true&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;false&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">AddNum</span><span class="p">()(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">20</span><span class="p">)</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl"><span class="p">}</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">basetxt</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="n">lefttxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is left txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="nf">toptxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is top txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="nf">righttxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is right txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="nf">lesttxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is lest txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">page1txt</span> <span class="o">:</span> <span class="k">public</span> <span class="n">basetxt</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="n">pagetxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is page1 txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">page2txt</span> <span class="o">:</span> <span class="k">public</span> <span class="n">basetxt</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="n">pagetxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is page2 txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">page3txt</span> <span class="o">:</span> <span class="k">public</span> <span class="n">basetxt</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="n">pagetxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is page3 txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">page4txt</span> <span class="o">:</span> <span class="k">public</span> <span class="n">basetxt</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">void</span> <span class="n">pagetxt</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;here is page4 txt&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test02</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page1txt</span><span class="p">().</span><span class="n">lefttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page1txt</span><span class="p">().</span><span class="n">toptxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page1txt</span><span class="p">().</span><span class="n">righttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page1txt</span><span class="p">().</span><span class="n">lesttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page1txt</span><span class="p">().</span><span class="n">pagetxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;--------------------&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="n">page2txt</span><span class="p">().</span><span class="n">lefttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page2txt</span><span class="p">().</span><span class="n">toptxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page2txt</span><span class="p">().</span><span class="n">righttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page2txt</span><span class="p">().</span><span class="n">lesttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page2txt</span><span class="p">().</span><span class="n">pagetxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;--------------------&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="n">page3txt</span><span class="p">().</span><span class="n">lefttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page3txt</span><span class="p">().</span><span class="n">toptxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page3txt</span><span class="p">().</span><span class="n">righttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page3txt</span><span class="p">().</span><span class="n">lesttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page3txt</span><span class="p">().</span><span class="n">pagetxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;--------------------&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">      
</span></span><span class="line"><span class="cl">    <span class="n">page4txt</span><span class="p">().</span><span class="n">lefttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page4txt</span><span class="p">().</span><span class="n">toptxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page4txt</span><span class="p">().</span><span class="n">righttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page4txt</span><span class="p">().</span><span class="n">lesttxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">page4txt</span><span class="p">().</span><span class="n">pagetxt</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;--------------------&#34;</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl"><span class="p">}</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">    <span class="n">test01</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="n">test02</span><span class="p">();</span>  
</span></span><span class="line"><span class="cl">    <span class="n">system</span><span class="p">(</span><span class="s">&#34;pause&#34;</span><span class="p">);</span>  
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h3 id="运算符重载">运算符重载
</h3><p>运算符重载一般用于自定义类之间的运算，可以简化代码，提高代码可读性。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span><span class="lnt">42
</span><span class="lnt">43
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt;   
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>   
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>  
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">m_a</span><span class="p">;</span>   
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">m_b</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl">    <span class="c1">// 类的构造函数，初始化成员变量m_a和m_b为10  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span><span class="p">()</span> <span class="o">:</span><span class="n">m_a</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">m_b</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="p">{}</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl">    <span class="c1">// 重载+运算符，使其能够用于myclass类型的对象  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="c1">// 这个函数接受一个myclass类型的对象作为参数，并返回一个新的myclass对象  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">myclass</span> <span class="k">operator</span><span class="o">+</span><span class="p">(</span><span class="n">myclass</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">        <span class="n">myclass</span> <span class="n">temp</span><span class="p">;</span> <span class="c1">// 创建一个临时的myclass对象，用于存储运算结果  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="c1">// 将当前对象的m_a与传入对象的m_a相加，结果赋值给临时对象的m_a  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">temp</span><span class="p">.</span><span class="n">m_a</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_a</span> <span class="o">+</span> <span class="n">p</span><span class="p">.</span><span class="n">m_a</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="c1">// 将当前对象的m_b与传入对象的m_b相加，结果赋值给临时对象的m_b  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>        <span class="n">temp</span><span class="p">.</span><span class="n">m_b</span> <span class="o">=</span> <span class="k">this</span><span class="o">-&gt;</span><span class="n">m_b</span> <span class="o">+</span> <span class="n">p</span><span class="p">.</span><span class="n">m_b</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">        <span class="k">return</span> <span class="n">temp</span><span class="p">;</span> <span class="c1">// 返回临时对象，作为+运算符的结果  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="p">}</span>  
</span></span><span class="line"><span class="cl"><span class="p">};</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span> <span class="n">p1</span><span class="p">;</span>   
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span> <span class="n">p2</span><span class="p">;</span>   
</span></span><span class="line"><span class="cl">    <span class="n">myclass</span> <span class="n">p3</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="c1">// 使用重载的+运算符，将p1和p2相加，结果赋值给p3  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">p3</span> <span class="o">=</span> <span class="n">p1</span> <span class="o">+</span> <span class="n">p2</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;p3.m_a = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">p3</span><span class="p">.</span><span class="n">m_a</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;p3.m_b = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">p3</span><span class="p">.</span><span class="n">m_b</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl"><span class="p">}</span>  
</span></span><span class="line"><span class="cl">  
</span></span><span class="line"><span class="cl"><span class="c1">// 声明一个重载的左移运算符（&lt;&lt;）函数，用于将myclass类的对象信息输出到ostream类型的流中  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="n">ostream</span><span class="o">&amp;</span> <span class="k">operator</span><span class="o">&lt;&lt;</span><span class="p">(</span><span class="n">ostream</span><span class="o">&amp;</span> <span class="n">cout</span><span class="p">,</span> <span class="n">myclass</span><span class="o">&amp;</span> <span class="n">p</span><span class="p">)</span> <span class="p">{</span>  
</span></span><span class="line"><span class="cl">    <span class="c1">// 使用传入的ostream引用（在这个上下文中通常是cout），输出myclass对象的m_a成员变量的值  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;m_a = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">p</span><span class="p">.</span><span class="n">m_a</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="c1">// 输出一个换行符，以便将m_a和m_b的输出分开，增加输出的可读性  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="c1">// 继续使用cout输出myclass对象的m_b成员变量的值，并在值后面添加一个换行符  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;m_b = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">p</span><span class="p">.</span><span class="n">m_b</span> <span class="o">&lt;&lt;</span> <span class="n">endl</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl">    <span class="c1">// 返回传入的ostream引用，这里通常是cout，这样函数就可以支持连续的输出操作（链式调用）  
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>    <span class="k">return</span> <span class="n">cout</span><span class="p">;</span>  
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="解释">解释
</h4><ul>
<li><strong>类定义</strong>：<code>myclass</code> 类有两个成员变量 <code>m_a</code> 和 <code>m_b</code>，并通过构造函数初始化为 10。</li>
<li><strong>运算符重载</strong>：
<ul>
<li><code>operator+</code>：重载了加法运算符，使两个 <code>myclass</code> 对象可以相加。返回一个新的 <code>myclass</code> 对象，其成员变量是两个相加对象的对应成员变量的和。</li>
</ul>
</li>
<li><strong>main 函数</strong>：创建三个 <code>myclass</code> 对象 <code>p1</code>、<code>p2</code> 和 <code>p3</code>，使用重载的 <code>+</code> 运算符将 <code>p1</code> 和 <code>p2</code> 相加，并将结果赋给 <code>p3</code>，最后输出 <code>p3</code> 的成员变量值。</li>
<li><strong>输出运算符重载</strong>：重载了 <code>&lt;&lt;</code> 运算符，使 <code>myclass</code> 对象可以直接输出到标准输出流 <code>cout</code>。</li>
</ul>
<p>通过这种方式，自定义类的对象可以像基本数据类型一样进行运算和输出，提高了代码的可读性和易用性。</p>
<h4 id="注意事项">注意事项
</h4><ul>
<li>运算符重载不会改变运算符的优先级和结合性。</li>
<li>运算符重载函数可以是成员函数，也可以是友元函数或非成员函数，但一般成员函数和友元函数更为常见。</li>
<li>对于一些非成员函数运算符重载，可能需要将相关的类声明为友元类，以便访问类的私有或受保护成员。</li>
</ul>
<h4 id="总结">总结
</h4><p>运算符重载是 C++ 中的一个强大特性，允许开发者为自定义类型定义标准运算符的行为。通过重载运算符，可以使自定义类型的使用更加直观和方便。</p>
<h3 id="全局函数友元">全局函数友元
</h3><p>当在类（class）中声明友元（friend）函数后，便可以在全局函数中调用私有对象，示例如下：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	<span class="k">friend</span> <span class="kt">void</span> <span class="n">test01</span><span class="p">(</span><span class="n">myclass</span><span class="o">*</span> <span class="n">Myclass</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">	<span class="n">myclass</span><span class="p">()</span><span class="o">:</span><span class="n">age</span><span class="p">(</span><span class="mi">18</span><span class="p">),</span><span class="n">weight</span><span class="p">(</span><span class="mi">70</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="p">}</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="n">age</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">private</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="n">weight</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">(</span><span class="n">myclass</span><span class="o">*</span> <span class="n">Myclass</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	
</span></span><span class="line"><span class="cl">	<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;正在访问“age”&#34;</span>  <span class="o">&lt;&lt;</span> <span class="n">endl</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;age = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">Myclass</span><span class="o">-&gt;</span><span class="n">age</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;正在访问“weight”&#34;</span>  <span class="o">&lt;&lt;</span> <span class="n">endl</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;age = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">Myclass</span><span class="o">-&gt;</span><span class="n">weight</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">	<span class="n">myclass</span> <span class="n">p1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="n">test01</span><span class="p">(</span><span class="o">&amp;</span><span class="n">p1</span><span class="p">);</span><span class="c1">//在调用使用指针传输的函数时，传入的数据应为对象的地址或指针
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-cpp" data-lang="cpp"><span class="line"><span class="cl"><span class="cp">#include&lt;iostream&gt;
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">class</span> <span class="nc">myclass</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl"><span class="k">public</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	<span class="k">friend</span> <span class="kt">void</span> <span class="n">test01</span><span class="p">(</span><span class="n">myclass</span><span class="o">&amp;</span> <span class="n">Myclass</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">	<span class="n">myclass</span><span class="p">()</span><span class="o">:</span><span class="n">age</span><span class="p">(</span><span class="mi">18</span><span class="p">),</span><span class="n">weight</span><span class="p">(</span><span class="mi">70</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">	<span class="p">}</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="n">age</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="k">private</span><span class="o">:</span>
</span></span><span class="line"><span class="cl">	<span class="kt">int</span> <span class="n">weight</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">(</span><span class="n">myclass</span><span class="o">&amp;</span> <span class="n">Myclass</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">	
</span></span><span class="line"><span class="cl">	<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;正在访问“age”&#34;</span>  <span class="o">&lt;&lt;</span> <span class="n">endl</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;age = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">Myclass</span><span class="p">.</span><span class="n">age</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;正在访问“weight”&#34;</span>  <span class="o">&lt;&lt;</span> <span class="n">endl</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;age = &#34;</span> <span class="o">&lt;&lt;</span> <span class="n">Myclass</span><span class="p">.</span><span class="n">weight</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">(){</span>
</span></span><span class="line"><span class="cl">	<span class="n">myclass</span> <span class="n">p1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">	<span class="n">test01</span><span class="p">(</span><span class="n">p1</span><span class="p">);</span><span class="c1">//在调用使用引用传输的函数时，传入的数据应为对象本身
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>当成员函数访问其他类中私有成员时，应如此声明：</p>
<p><code>friend class01(成员函数所属的类)::void class01test ;</code></p>
<h2 id="stl容器简略写点">STL容器（简略写点）
</h2><h3 id="基本语法">基本语法
</h3><p>创建容器： <code>vector &lt;数据类型&gt; 容器名 {数组内容};</code></p>
<p>使用迭代器:<code>vector&lt;数据类型&gt;::iterators 地址名（类似于指针）= 取址函数 </code></p>
<p>*<em><font style="background-color:rgba(255, 255, 255, 0);">元素的选取：<em>地址名  ||  容器名.at(元素的序号)  ||  容器名[元素的序号]</font></em></em></p>
<p><strong><font style="background-color:rgba(255, 255, 255, 0);">取址函数（函数名之前加c会成为只读型）:</font></strong></p>
<div class="table-wrapper"><table>
<thead>
<tr>
<th>begin()</th>
<th>指向容器的第一个元素</th>
</tr>
</thead>
<tbody>
<tr>
<td>end()</td>
<td>指向元素的最后一个元素的后一位</td>
</tr>
<tr>
<td>rbegin()</td>
<td>倒序指向的begin()</td>
</tr>
<tr>
<td>rend()</td>
<td>倒序指向的end()</td>
</tr>
</tbody>
</table></div>
<p>容量函数：</p>
<div class="table-wrapper"><table>
<thead>
<tr>
<th>size()</th>
<th>容器内元素的个数</th>
</tr>
</thead>
<tbody>
<tr>
<td>empty()</td>
<td>若容器为空则返回true，否则返回false</td>
</tr>
<tr>
<td>reserve()</td>
<td>为容器预留不可访问的空间，减少容器追加空间的次数，降低运算复杂度（增大内存占用）</td>
</tr>
</tbody>
</table></div>
<p>修改函数：</p>
<div class="table-wrapper"><table>
<thead>
<tr>
<th>push_back</th>
<th>在容器尾追加元素</th>
</tr>
</thead>
<tbody>
<tr>
<td>pop_back</td>
<td>删除最后一个元素</td>
</tr>
<tr>
<td>insert</td>
<td>插入元素</td>
</tr>
<tr>
<td>erase</td>
<td>删除元素</td>
</tr>
<tr>
<td>clear</td>
<td>清空容器</td>
</tr>
<tr>
<td>swap</td>
<td>交换元素位置</td>
</tr>
<tr>
<td>assign</td>
<td>替换元素</td>
</tr>
<tr>
<td>emplace</td>
<td>更快的插入</td>
</tr>
<tr>
<td>emplace_back</td>
<td>更快的追加元素</td>
</tr>
</tbody>
</table></div>
<p>各种容器之间的用途与区别：</p>
<p>list ：双向链表，可以快速的插入与删除某一元素，遍历和空间占用差</p>
<p>vector ：动态指针，可以自动扩容，遍历最快</p>
<p>deque ：可以在头尾分别操作元素，但是速度慢于vector</p>
<p>stack ：类似于羽毛球筒，只能调用和访问最顶上的数据，数据存储在栈区</p>
<p>queue ：类似于队伍，只能访问两端元素，不可遍历，只能向一端插入一端删除。</p>
<p>set与multiset ：数组，可以遍历，会自动排序，前者元素不可重复，后者可以.</p>
<h3 id="stl容器的一些示例">STL容器的一些示例
</h3><h4 id="大数阶乘容器">大数阶乘（容器）
</h4><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-c++" data-lang="c++"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">print</span><span class="p">(</span><span class="kt">int</span> <span class="n">val</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="n">val</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">temp1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">temp2</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">n</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&#34;请输入n： &#34;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">cin</span> <span class="o">&gt;&gt;</span> <span class="n">n</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="n">v1</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;=</span> <span class="n">n</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">j</span> <span class="o">&lt;</span> <span class="n">v1</span><span class="p">.</span><span class="n">size</span><span class="p">();</span> <span class="n">j</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="o">=</span> <span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">j</span><span class="p">)</span><span class="o">*</span><span class="n">i</span> <span class="o">+</span> <span class="n">temp2</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">temp2</span> <span class="o">=</span> <span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">j</span><span class="p">)</span><span class="o">/</span><span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">j</span><span class="p">)</span> <span class="o">=</span> <span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">j</span><span class="p">)</span><span class="o">%</span><span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span> <span class="p">(</span><span class="n">temp2</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">v1</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">temp2</span><span class="o">%</span><span class="mi">10</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">temp2</span> <span class="o">=</span> <span class="n">temp2</span><span class="o">/</span><span class="mi">10</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">v1</span><span class="p">.</span><span class="n">size</span><span class="p">()</span><span class="o">-</span><span class="mi">1</span><span class="p">;</span><span class="n">i</span><span class="o">&gt;=</span><span class="mi">0</span><span class="p">;</span> <span class="n">i</span><span class="o">--</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">print</span><span class="p">(</span><span class="n">v1</span><span class="p">.</span><span class="n">at</span><span class="p">(</span><span class="n">i</span><span class="p">));</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">    <span class="n">system</span><span class="p">(</span><span class="s">&#34;pause&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="基于vector容器的素数查找">基于vector容器的素数查找
</h4><div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-c++" data-lang="c++"><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;algorithm&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp">
</span></span></span><span class="line"><span class="cl"><span class="cp"></span><span class="k">using</span> <span class="k">namespace</span> <span class="n">std</span><span class="p">;</span>
</span></span><span class="line"><span class="cl"><span class="kt">void</span> <span class="nf">test01</span><span class="p">(</span><span class="kt">int</span> <span class="n">n</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="n">v</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">flag</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="kt">int</span> <span class="n">times</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">n</span><span class="o">+</span><span class="mi">1</span><span class="p">;</span> <span class="n">i</span><span class="o">++</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">        <span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;::</span><span class="n">iterator</span> <span class="n">it</span> <span class="o">=</span> <span class="n">v</span><span class="p">.</span><span class="n">begin</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">        <span class="k">while</span><span class="p">(</span><span class="n">it</span> <span class="o">!=</span> <span class="n">v</span><span class="p">.</span><span class="n">end</span><span class="p">())</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span><span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="o">*</span><span class="n">it</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">flag</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">it</span><span class="o">++</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="n">flag</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="k">if</span><span class="p">(</span><span class="n">i</span> <span class="o">!=</span> <span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">                <span class="n">v</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">i</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="p">}</span>
</span></span><span class="line"><span class="cl">            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&#34; &#34;</span><span class="o">&lt;&lt;</span><span class="n">i</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">times</span><span class="o">++</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="k">if</span><span class="p">(</span><span class="n">times</span> <span class="o">==</span> <span class="mi">4</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">times</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">        <span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="n">flag</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl"><span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span><span class="kt">int</span> <span class="n">times</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">cout</span><span class="o">&lt;&lt;</span><span class="s">&#34;请输入你所需要查找的素数范围（0~n）：&#34;</span><span class="o">&lt;&lt;</span><span class="n">endl</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">cin</span><span class="o">&gt;&gt;</span><span class="n">times</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">            <span class="n">test01</span><span class="p">(</span><span class="n">times</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">            <span class="n">system</span><span class="p">(</span><span class="s">&#34;pause&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">           <span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div>
</section>


    <footer class="article-footer">
    

    
    <section class="article-copyright">
        <svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-copyright" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
  <path stroke="none" d="M0 0h24v24H0z"/>
  <circle cx="12" cy="12" r="9" />
  <path d="M14.5 9a3.5 4 0 1 0 0 6" />
</svg>



        <span>Licensed under CC BY-NC-SA 4.0</span>
    </section>
    </footer>


    
</article>

    

    

<aside class="related-content--wrapper">
    <h2 class="section-title">相关文章</h2>
    <div class="related-content">
        <div class="flex article-list--tile">
            
                
<article class="has-image">
    <a href="/p/suanfa/">
        
        
            <div class="article-image">
                
                    <img src="/images/studying1.jpg" loading="lazy" data-key="suanfa" data-hash="/images/studying1.jpg"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">数据结构算法积累</h2>
        </div>
    </a>
</article>

            
                
<article class="has-image">
    <a href="/p/wdsj/">
        
        
            <div class="article-image">
                
                    <img src="/images/plc.png" loading="lazy" data-key="wdsj" data-hash="/images/plc.png"/>
                
            </div>
        

        <div class="article-details">
            <h2 class="article-title">我的世界JAVA版非局域网远程联机教程</h2>
        </div>
    </a>
</article>

            
        </div>
    </div>
</aside>

     
    
        
    <div class="disqus-container">
    <div id="disqus_thread"></div>
<script>
    window.disqus_config = function () {
    
    
    
    };
    (function() {
        if (["localhost", "127.0.0.1"].indexOf(window.location.hostname) != -1) {
            document.getElementById('disqus_thread').innerHTML = 'Disqus comments not available by default when the website is previewed locally.';
            return;
        }
        var d = document, s = d.createElement('script'); s.async = true;
        s.src = '//' + "hugo-theme-stack" + '.disqus.com/embed.js';
        s.setAttribute('data-timestamp', +new Date());
        (d.head || d.body).appendChild(s);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="https://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>

<style>
    .disqus-container {
        background-color: var(--card-background);
        border-radius: var(--card-border-radius);
        box-shadow: var(--shadow-l1);
        padding: var(--card-padding);
    }
</style>

<script>
    window.addEventListener('onColorSchemeChange', (e) => {
        if (typeof DISQUS == 'object') {
            DISQUS.reset({
                reload: true
            });
        }
    })
</script>

    

    <footer class="site-footer">
    <section class="copyright">
        &copy; 
        
        2024 VenKing_Ryao-HEUN
    </section>
    
    <section class="powerby">
        使用 <a href="https://gohugo.io/" target="_blank" rel="noopener">Hugo</a> 构建 <br />
        主题 <b><a href="https://github.com/CaiJimmy/hugo-theme-stack" target="_blank" rel="noopener" data-version="3.29.0">Stack</a></b> 由 <a href="https://jimmycai.com" target="_blank" rel="noopener">Jimmy</a> 设计
    </section>
</footer>


    
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    
    <div class="pswp__bg"></div>

    
    <div class="pswp__scroll-wrap">

        
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                
                
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"integrity="sha256-ePwmChbbvXbsO02lbM3HoHbSHTHFAeChekF1xKJdleo="crossorigin="anonymous"
                defer
                >
            </script><script 
                src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"integrity="sha256-UKkzOn/w1mBxRmLLGrSeyB4e1xbrp4xylgAWb3M42pU="crossorigin="anonymous"
                defer
                >
            </script><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.min.css"crossorigin="anonymous"
            ><link 
                rel="stylesheet" 
                href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css"crossorigin="anonymous"
            >

            </main>
        </div>
        <script 
                src="https://cdn.jsdelivr.net/npm/node-vibrant@3.1.6/dist/vibrant.min.js"integrity="sha256-awcR2jno4kI5X0zL8ex0vi2z&#43;KMkF24hUW8WePSA9HM="crossorigin="anonymous"
                
                >
            </script><script type="text/javascript" src="/ts/main.fe40349e407ba5b8d0df08d300ffc38d0d30eed264b452958879f7cb3a13a7ed.js" defer></script>
<script>
    (function () {
        const customFont = document.createElement('link');
        customFont.href = "https://fonts.googleapis.com/css2?family=Lato:wght@300;400;700&display=swap";

        customFont.type = "text/css";
        customFont.rel = "stylesheet";

        document.head.appendChild(customFont);
    }());
</script>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">
<div id="aplayer"></div>
<script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>

<script>
const ap = new APlayer({
    container: document.getElementById('aplayer'),
    audio: []
});
</script>

    </body>
</html>
